/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package workstationbookerweb.fragmentos;

import Fachada.Fachada;
import VO.EstacionTrabajoVO;
import com.sun.rave.web.ui.appbase.AbstractFragmentBean;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.Checkbox;
import com.sun.webui.jsf.component.Label;
import com.sun.webui.jsf.component.TextArea;
import com.sun.webui.jsf.component.TextField;
import javax.faces.FacesException;
import workstationbookerweb.SessionBean1;
import workstationbookerweb.ApplicationBean1;
import workstationbookerweb.RequestBean1;

/**
 * <p>Fragment bean that corresponds to a similarly named JSP page
 * fragment.  This class contains component definitions (and initialization
 * code) for all components that you have defined on this fragment, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version fragmentoActualizarEstacion.java
 * @version Created on 31/05/2011, 10:21:30 PM
 * @author Julian Pena G
 */

public class fragmentoActualizarEstacion extends AbstractFragmentBean {

    private Fachada fachada = Fachada.getInstance();
    private EstacionTrabajoVO etvo;
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization. <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        Long id = (Long)getValue("#{sessionScope.idEstacion}");
        long idEstacion = id.longValue();
        etvo = fachada.leerEstacionTrabajo(idEstacion);
        if(etvo==null){
            mensajeL.setText("no se encontro la estacion");
            aceptarB.setDisabled(true);
            return;
        }
        numTF.setText(etvo.getNumero());
        salaTF.setText(etvo.getSala());
        if(etvo.isDisponibilidad()){
            disponibilidadCB.setValue(true);
        }
        capacidadTF.setText(etvo.getCapacidad());
        if(etvo.isPc()){
           pcCB.setValue(true);
        }
        if(etvo.isToma()){
           tomaCB.setValue(true);
        }
        descripcionTA.setText(etvo.getDescripcion());

    }
    private TextField numTF = new TextField();

    public TextField getNumTF() {
        return numTF;
    }

    public void setNumTF(TextField tf) {
        this.numTF = tf;
    }
    private TextField salaTF = new TextField();

    public TextField getSalaTF() {
        return salaTF;
    }

    public void setSalaTF(TextField tf) {
        this.salaTF = tf;
    }
    private Checkbox disponibilidadCB = new Checkbox();

    public Checkbox getDisponibilidadCB() {
        return disponibilidadCB;
    }

    public void setDisponibilidadCB(Checkbox c) {
        this.disponibilidadCB = c;
    }
    private TextField capacidadTF = new TextField();

    public TextField getCapacidadTF() {
        return capacidadTF;
    }

    public void setCapacidadTF(TextField tf) {
        this.capacidadTF = tf;
    }
    private Checkbox pcCB = new Checkbox();

    public Checkbox getPcCB() {
        return pcCB;
    }

    public void setPcCB(Checkbox c) {
        this.pcCB = c;
    }
    private Checkbox tomaCB = new Checkbox();

    public Checkbox getTomaCB() {
        return tomaCB;
    }

    public void setTomaCB(Checkbox c) {
        this.tomaCB = c;
    }
    private Button aceptarB = new Button();

    public Button getAceptarB() {
        return aceptarB;
    }

    public void setAceptarB(Button b) {
        this.aceptarB = b;
    }
    private TextArea descripcionTA = new TextArea();

    public TextArea getDescripcionTA() {
        return descripcionTA;
    }

    public void setDescripcionTA(TextArea ta) {
        this.descripcionTA = ta;
    }
    private Label mensajeL = new Label();

    public Label getMensajeL() {
        return mensajeL;
    }

    public void setMensajeL(Label l) {
        this.mensajeL = l;
    }
    // </editor-fold>

    public fragmentoActualizarEstacion() {
    }

    /**
     * <p>Callback method that is called whenever a page containing
     * this page fragment is navigated to, either directly via a URL,
     * or indirectly via page navigation.  Override this method to acquire
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        
        // <editor-fold defaultstate="collapsed" desc="Visual-Web-managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called.  Override this
     * method to release resources acquired in the <code>init()</code>
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    public String aceptarB_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        boolean valido = validarCampos();
        if(!valido){
            mensajeL.setText("hay campos vacios");
            return null;
        }

        int num = 0;
        try {
            Integer algo = (Integer)numTF.getText();
            num = algo.intValue();
        } catch (Exception E) {
            mensajeL.setText("deve ser un valor numerico");
            return null;
        }
        etvo.setNumero(num);
        Integer algo2 = (Integer)capacidadTF.getText();
        num = algo2.intValue();
        etvo.setCapacidad(num);
        if(disponibilidadCB.isChecked()){
            etvo.setDisponibilidad(true);
        }
        else{
            etvo.setDisponibilidad(false);
        }
        if(pcCB.isChecked()){
            etvo.setPc(true);
        }
        else{
            etvo.setPc(false);
        }
        if(tomaCB.isChecked()){
            etvo.setToma(true);
        }
        else{
            etvo.setToma(false);
        }
        String descripcion = (String)descripcionTA.getText();
        etvo.setDescripcion(descripcion);
        EstacionTrabajoVO vo = fachada.actualizarEstacion(etvo);
        if(vo==null){
            mensajeL.setText("el objeto no fue actualizado");
            return null;
        }
        return "/faces/paginas/AdministrarSala.jsp";
    }

    public String cancelarB_action() {
        return "/faces/paginas/AdministrarSala.jsp";
    }

    private boolean validarCampos() {
        if(numTF.getText()==null || numTF.getText().equals("")) return false;
        if(capacidadTF.getText()==null || capacidadTF.getText().equals("")) return false;
        return true;
    }

    public String cerrarSesionB_action() {
        return "/faces/paginas/index.jsp";
    }

}
